<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BUIOperation
 *
 * @author ID59C
 */
class BUIOperation extends BUINode {

    //public $views = null;
    //public $layoutView = null;
    //protected $startScreen = null;
    //protected $startScreenFlow = null;

    public function __construct($node = NULL) {
        parent::__construct($node);
        $this->operation = $node;
    }

    public function onRender($data = null) {

        parent::onRender();

        if ($this->views == NULL)
            $this->views = array();

        $this->views['operationTopBar'] = array('view' => 'operation_topBar', 'uiObj' => $this);

        //add controls
        /*
          if (Blyn::app()->getUIManager()->getCurrentScreenFlow() != NULL) {
          $uiScnFlow = Blyn::app()->getUIManager()->getCurrentScreenFlow();
          } else {
          $uiScnFlow = new BUIScreenFlow();
          }
          $this->addControl($uiScnFlow);
         * 
         */


        if (Blyn::app()->getUIManager()->getCurrentScreen() != NULL) {
            $uiScreen = Blyn::app()->getUIManager()->getCurrentScreen();
        } else {
            $uiScreen = new BUIScreen();
        }
        $this->addControl($uiScreen);
    }

    public function getUrl($data = NULL) {

        $ajaxUrl = $this->getAjaxUrl($data);

        if ($ajaxUrl != NULL)
            return $ajaxUrl;

        $spaceId = Blyn::app()->getActiveSpace()->getId();
        $appId = $this->getNode()->getApp()->getId();
        $oppId = $this->getNode()->getId();

        return $this->buildUrl($spaceId, $appId, $oppId, $data);
    }

    public function getAjaxLink($name, $data = array(), $sender = NULL, $route = 'site/index', $updateControl = NULL) {

        if ($sender == NULL)
            $sender = $this;

        $currentApp = $this->getNode()->getCurrentApp();
        $activeApp = Blyn::app()->getActiveApp();

        $currentSpace = $currentApp->getCurrentSpace();
        $activeSpace = Blyn::app()->getActiveSpace();

        if ($currentSpace->getId() != $activeSpace->getId()) {
            return Blyn::app()->getUIManager()->getAjaxLink($name, $data, $sender);
        } else if ($currentApp->getId() != $activeApp->getId()) {
            return Blyn::app()->getActiveSpace()->getUIManager()->getAjaxLink($name, $data, $sender);
        } else if (Blyn::app()->getActiveOperation() == NULL || ($sender->getNode()->getId() != Blyn::app()->getActiveOperation()->getId())) {
            return Blyn::app()->getActiveApp()->getUIManager()->getAjaxLink($name, $data, $sender);
        } else {
            return parent::getAjaxLink($name, $data, $sender, $route, $updateControl);
        }
        /*
          $uiApp = Blyn::app()->getActiveApp()->getUIManager();
          //return $uiApp->getAjaxLink($name, $data);
          $addingView = array(
          'view' => $uiApp->getLayoutView(),
          'data' => array(
          'control' => $uiApp->parseIntoArray(),
          'sender' => $sender->parseIntoArray(),
          ));
          $currentSpace = $this->getNode()->getCurrentApp()->getCurrentSpace();
          if ($currentSpace->getId() != Blyn::app()->getActiveSpace()->getId())
          return Blyn::app()->getUIManager()->getAjaxLink($name, $data, $this);
          return parent::createAjaxLink($name, $uiApp->getUrl(), BUIBase::ajaxContainter_App, $addingView, $data);
         * 
         */
    }

    /*
      public function getAjaxLink($name, $data = NULL) {
      return parent::createAjaxLink($name, $this->getUrl(), BUIBase::ajaxContainter_Space, Blyn::app()->getActiveSpace()->getUIManager()->getLayoutView(), $data);
      }
     * 
     */

    public function getStartScreen($data = NULL) {

        $op = $this->getNode();

        $operationName = $op->getName();

        $app = $op->getApp();

        if ($this->startScreen == NULL) {

            $startScreenConfig = $app->getConfigManager()->getConfig('appMeta.operations.' . $operationName . '.startScreen');
            if ($startScreenConfig instanceof BConfig) {
                $screenClass = $startScreenConfig->getValue();
                if (is_string($screenClass) && class_exists($screenClass))
                    $this->startScreen = new $screenClass();
                else
                    $this->startScreen = new BUIScreen ();
            }
            elseif ($startScreenConfig instanceof BConfigGroup) {
                $screenClass = $startScreenConfig->getConfig('class');
                $viewsConfig = $startScreenConfig->getConfig('views');
                $startScreen = NULL;
                if ($viewsConfig instanceof BConfig)
                    $views = array('view1' => $viewsConfig->getValue());
                if ($viewsConfig instanceof BConfigGroup)
                    $views = $viewsConfig->getConfigList();
                if ($screenClass != NULL)
                    $startScreen = new $screenClass;
                else
                    $startScreen = new BUIScreen();

                if ($startScreen instanceof BUIScreen) {
                    $startScreen->addViews($views);
                }
                $this->startScreen = $startScreen;
            } elseif (is_string($startScreenConfig)) {
                $screenClass = $startScreenConfig;
                if (class_exists($screenClass))
                    $this->startScreen = new $screenClass();
            } elseif (is_array($startScreenConfig)) {
                if (isset($startScreenConfig['class']) && $startScreenConfig['class'] != NULL)
                    $startScreenClass = $startScreenConfig['class'];
                else
                    $startScreenClass = 'BUIScreen';
                $startScreen = new $startScreenClass();
                if (isset($startScreenConfig['views']) && $startScreenConfig['views'] != NULL) {
                    $views = $startScreenConfig['views'];
                    $startScreen->addViews($views);
                    $this->startScreen = $startScreen;
                }
            }
        }

        if (is_string($this->startScreen) && $this->startScreen != NULL) {
            $this->startScreen = $this->getScreen($this->startScreen);
            //$this->startScreen->setLayoutView($layoutView);
        }

        if ($this->startScreen instanceof BUIScreen)
            return $this->startScreen;
    }

    public function getStartScreenFlow($data = NULL) {
        $op = $this->getNode();

        if ($this->startScreenFlow != NULL)
            return $this->getScreenFlow($this->startScreenFlow);

        $configStartFlow = $op->getApp()->getConfigManager()->getConfig('appMeta.operations.' . $op->getName() . '.startScreenFlow');
        if ($configStartFlow != NULL) {
            $this->startScreenFlow = $this->getScreenFlow($configStartFlow);
            return $this->startScreenFlow;
        }

        return FALSE;
    }
    
    public function getAjaxContainerId($data = NULL) {
        return 'b_show_operation_content';
    }
    
    public function getAngularControllerDefine($data = NULL) {
        return 'bngOperationController';
    }

}

?>
